package cn.edu.sgu.www.mhxysy.service.children.impl;

import cn.edu.sgu.www.mhxysy.dto.children.ChildrenJiadianDTO;
import cn.edu.sgu.www.mhxysy.entity.children.ChildrenAttribute;
import cn.edu.sgu.www.mhxysy.entity.children.ChildrenJiadian;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuAttribute;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuJiadian;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.children.ChildrenAttributeMapper;
import cn.edu.sgu.www.mhxysy.mapper.children.ChildrenJiadianMapper;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.children.ChildrenJiadianService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChildrenJiadianServiceImpl implements ChildrenJiadianService {

    private final ChildrenJiadianMapper childrenJiadianMapper;
    private final ChildrenAttributeMapper childrenAttributeMapper;

    @Autowired
    public ChildrenJiadianServiceImpl(
            ChildrenJiadianMapper childrenJiadianMapper, 
            ChildrenAttributeMapper childrenAttributeMapper) {
        this.childrenJiadianMapper = childrenJiadianMapper;
        this.childrenAttributeMapper = childrenAttributeMapper;
    }

    @Override
    public void update(ChildrenJiadianDTO childrenJiadianDTO) {
        String childrenId = childrenJiadianDTO.getChildrenId();

        // 查询子女加点信息
        QueryWrapper<ChildrenJiadian> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("children_id", childrenId);

        ChildrenJiadian jiadian = childrenJiadianMapper.selectOne(queryWrapper);

        if (jiadian == null) {
            throw new GlobalException(ResponseCode.BAD_REQUEST, "子女加点不存在！");
        }

        String jiadianId = jiadian.getId();

        // 1、修改子女加点
        ChildrenJiadian childrenJiadian = new ChildrenJiadian();

        childrenJiadian.setId(jiadianId);
        childrenJiadian.setMoli(childrenJiadianDTO.getMoli());
        childrenJiadian.setTizhi(childrenJiadianDTO.getTizhi());
        childrenJiadian.setNaili(childrenJiadianDTO.getNaili());
        childrenJiadian.setMinjie(childrenJiadianDTO.getMinjie());
        childrenJiadian.setLiliang(childrenJiadianDTO.getLiliang());

        childrenJiadianMapper.updateById(childrenJiadian);

        // 2、修改子女属性
        ChildrenAttribute childrenAttribute = new ChildrenAttribute();

        childrenAttribute.setQixue(childrenJiadianDTO.getQixue());
        childrenAttribute.setWlsh(childrenJiadianDTO.getWlsh());
        childrenAttribute.setWlfy(childrenJiadianDTO.getWlfy());
        childrenAttribute.setFssh(childrenJiadianDTO.getFssh());
        childrenAttribute.setFsfy(childrenJiadianDTO.getFsfy());
        childrenAttribute.setSudu(childrenJiadianDTO.getSudu());

        // 通过子女ID和子女加点ID修改子女属性
        UpdateWrapper<ChildrenAttribute> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("children_id", childrenId);
        updateWrapper.eq("jiadian_id", jiadianId);

        childrenAttributeMapper.update(childrenAttribute, updateWrapper);
    }

    @Override
    public ChildrenJiadian selectByChildrenId(String childrenId) {
        QueryWrapper<ChildrenJiadian> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("children_id", childrenId);

        return childrenJiadianMapper.selectOne(queryWrapper);
    }

}